
//
//  ExercisesNormalAlertView.m
//  ShowYork
//
//  Created by hxh on 2020/9/10.
//  Copyright © 2020 hxh. All rights reserved.
//
typedef void(^doneBlock)(void);
#import "ExercisesNormalAlertView.h"

@interface ExercisesNormalAlertView ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *messageLb;
@property (nonatomic,strong)UIButton *cancelBtn;
@property (nonatomic,strong)UIButton *confirmBtn;
@property (nonatomic,copy)doneBlock cancelDone;
@property (nonatomic,copy)doneBlock confirmDone;
@end

@implementation ExercisesNormalAlertView

- (instancetype)initWithAlertTitle:(NSString *)alertTitle message:(NSString *)message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString *)cancelTitle confirmAciton:(void (^)(void))confirmBlock cancelAction:(void (^)(void))cancelBlock{
    if (self = [super init]) {
        [self setUI];
        self.titleLb.text = alertTitle;
        self.messageLb.text = message;
        [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
        [self.confirmBtn setTitle:confirmTitle forState:UIControlStateNormal];
        
        if (confirmBlock) {
            self.confirmDone = ^{
                confirmBlock();
            };
        }
        if (cancelBlock) {
            self.cancelDone = ^{
                cancelBlock();
            };
        }
        
    }
    return self;
}
- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    self.backgroundColor = RGBA(0, 0, 0, 0.4);
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    self.containView.layer.cornerRadius = 14;
    self.containView.layer.masksToBounds = YES;
    [self addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(45);
        make.right.equalTo(self).offset(-45);
        make.height.mas_equalTo(self.containView.mas_width).multipliedBy(0.54);
        make.center.equalTo(self);
    }];
    
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.text = @"您之前已经做过此试卷，请问是重新开始还是继续上次进度？";
    self.titleLb.textAlignment = NSTextAlignmentCenter;
    self.titleLb.numberOfLines = 0;
    self.titleLb.textColor = RGBA(35,24, 21, 1);
    self.titleLb.font = [UIFont boldSystemFontOfSize:18];
    [self.containView addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.containView).offset(25);
        make.left.equalTo(self.containView).offset(18);
        make.right.equalTo(self.containView).offset(-18);
    }];
    
    self.messageLb = [[UILabel alloc]init];
    self.messageLb.text = @"(重做将清空上次答题记录)";
    self.messageLb.textAlignment = NSTextAlignmentCenter;
    self.messageLb.numberOfLines = 0;
    self.messageLb.textColor = RGBA(102, 102, 102, 1);
    self.messageLb.font = font(14);
    [self.containView addSubview:self.messageLb];
    [self.messageLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.titleLb);
        make.top.equalTo(self.titleLb.mas_bottom).offset(8);
    }];

    self.cancelBtn = [[UIButton alloc]init];
    [self.cancelBtn setTitle:@"重做" forState:UIControlStateNormal];
    [self.cancelBtn setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
    self.cancelBtn.titleLabel.font = font(14);
    self.cancelBtn.layer.cornerRadius = 17;
    self.cancelBtn.layer.borderWidth = 1;
    self.cancelBtn.layer.borderColor = RGBA(221, 221, 221, 1).CGColor;
    self.cancelBtn.layer.masksToBounds = YES;
    [self.cancelBtn addTarget:self action:@selector(cancelMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.cancelBtn];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120, 34));
        make.centerX.equalTo(self.containView).offset(-68);
        make.bottom.equalTo(self.containView).offset(-13);
    }];
    
    self.confirmBtn = [[UIButton alloc]init];
    [self.confirmBtn setTitle:@"继续" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmBtn.titleLabel.font = font(14);
    self.confirmBtn.layer.cornerRadius = 17;
    self.confirmBtn.layer.masksToBounds = YES;
    self.confirmBtn.backgroundColor = [RPAppManager themColor];
    [self.confirmBtn addTarget:self action:@selector(confirmMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.confirmBtn];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120, 34));
        make.centerX.equalTo(self.containView).offset(68);
        make.bottom.equalTo(self.containView).offset(-13);
    }];
}
- (void)cancelMethod{
    if (self.cancelDone) {
        self.cancelDone();
    }
    [self removeFromSuperview];
}
- (void)confirmMethod{
    if (self.confirmDone) {
        self.confirmDone();
    }
    [self removeFromSuperview];
}


- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}
- (void)dismiss{
    [self removeFromSuperview];
}

@end
